package com.backend.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.exception.ModelCoverDTOFailedException;
import com.common.exception.ModelNotFoundException;
import com.common.model.Category;
import com.common.service.CategoryService;
import com.util.CommonPage;
import com.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

@RestController
@Api(tags = "CategoryController", value = "文章分类接口")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation(value = "获取分类")
    @RequestMapping(value = "/category", method = RequestMethod.GET)
    public CommonResult<CommonPage<Category>> getCategory(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        IPage<Category> categories = categoryService.selectPage(page, pageSize);

        return CommonResult.success(CommonPage.restPage(categories));
    }

    @ApiOperation(value = "创建分类")
    @RequestMapping(value = "/category", method = RequestMethod.POST)
    public CommonResult createCategory(
            @RequestBody @Valid Category category,
            BindingResult bindingResult
    ) {

        if (bindingResult.hasErrors()) {
            return CommonResult.failed(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }

        int result = categoryService.insert(category);

        if (result > 0) {
            return CommonResult.success("操作成功");
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("删除分类")
    @RequestMapping(value = "/category/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteCategory(
            @PathVariable("id") Long id
    ) {

        try {
            int result = categoryService.delete(id);
            if (result > 0) {
                return CommonResult.success("操作成功了呢");
            } else {
                return CommonResult.failed("啊哦，失败了 看看日志吧");
            }
        } catch (ModelNotFoundException e) {
            return CommonResult.failed("找不到要删除的分类呢");
        }
    }

    @ApiOperation("更新分类")
    @RequestMapping(value = "/category/{id}", method = RequestMethod.PUT)
    public CommonResult putCategory(
            @PathVariable("id") Long id,
            @RequestBody @Valid Category category
    ) {
        try {
            int result = categoryService.update(id, category);
            if (result > 0) {
                return CommonResult.success("操作成功了呢");
            } else {
                return CommonResult.failed("啊哦，失败了 看看日志吧");
            }
        } catch (ModelNotFoundException e) {
            return CommonResult.failed("找不到要修改的分类呢");
        } catch (ModelCoverDTOFailedException e) {
            return CommonResult.failed("转换DTO失败 是不是有啥错误数据了呢");
        }
    }

    @ApiOperation("搜索分类列表")
    @RequestMapping(value = "/search/category", method = RequestMethod.GET)
    public CommonResult<Map<Long, String>> searchCategory(
            @RequestParam(value = "title", required = false) String title
    ) {
        Map<Long, String> result = categoryService.searchCategoryList(title);
        return CommonResult.success(result);
    }

}
